import tkinter as tk
from tkinter import filedialog, messagebox

from utils import main_process  # 如果没有此模块，请注释掉避免错误


def select_file(entry_widget):
    """选择文件并更新输入框"""
    file_path = filedialog.askopenfilename()
    if file_path:
        entry_widget.delete(0, tk.END)
        entry_widget.insert(0, file_path)


def get_all_inputs():
    """获取所有输入数据的接口函数"""


    return {
        "name_file": entry_file1.get(),
        "original_data": entry_file2.get(),
        "leave_file": entry_file3.get(),
        "holidays_file": entry_file4.get(),
        "rest_day": var_day.get(),
        "year": int(entry_year.get()),
        "month": int(entry_month.get())
    }


def validate_inputs(input_data):
    """验证输入数据是否有效"""
    if not all([input_data["name_file"], input_data["original_data"],
                input_data["leave_file"], input_data["holidays_file"],
                input_data["rest_day"], input_data["year"], input_data["month"]]):
        messagebox.showwarning("警告", "请填写所有必填字段！")
        return False

    try:
        year = int(input_data["year"])
        month = int(input_data["month"])
        if month < 1 or month > 12:
            raise ValueError("月份必须在1-12之间")
    except ValueError as e:
        messagebox.showerror("错误", f"请输入有效的年份和月份！\n{str(e)}")
        return False

    # 节日文件非必填，仅提示
    if not input_data["holidays_file"]:
        messagebox.showinfo("提示", "未选择节日文件，将视为无节日。")

    return True


def generate_output():
    """生成按钮的回调函数"""
    input_data = get_all_inputs()
    print(input_data)

    if not validate_inputs(input_data):
        return

    result = "已选择以下内容：\n\n"
    result += f"名单文件: {input_data['name_file']}\n"
    result += f"班制班别文件: {input_data['original_data']}\n"
    result += f"请假文件: {input_data['leave_file']}\n"
    result += f"节日文件: {input_data['holidays_file'] or '未选择'}\n"
    result += f"休息日: {input_data['rest_day']}\n"
    result += f"年份: {input_data['year']}, 月份: {input_data['month']}"

    messagebox.showinfo("生成结果", result)

    # 你也可以在这里调用 main_process(input_data)
    # main_process(input_data)

    main_process.main(**input_data)

    return input_data


# 创建主窗口
root = tk.Tk()
root.title("数据生成工具")
root.geometry("550x450")

# 设置网格布局权重
for i in range(7):
    root.grid_rowconfigure(i, weight=1)
for i in range(3):
    root.grid_columnconfigure(i, weight=1)

# 文件选择1 - 名单文件
tk.Label(root, text="选择名单文件:").grid(row=0, column=0, padx=10, pady=5, sticky="w")
entry_file1 = tk.Entry(root, width=45)
entry_file1.grid(row=0, column=1, padx=5, pady=5, sticky="ew")
tk.Button(root, text="浏览...", command=lambda: select_file(entry_file1)).grid(row=0, column=2, padx=5, pady=5)

# 文件选择2 - 班制班别文件
tk.Label(root, text="选择班制班别文件:").grid(row=1, column=0, padx=10, pady=5, sticky="w")
entry_file2 = tk.Entry(root, width=45)
entry_file2.grid(row=1, column=1, padx=5, pady=5, sticky="ew")
tk.Button(root, text="浏览...", command=lambda: select_file(entry_file2)).grid(row=1, column=2, padx=5, pady=5)

# 文件选择3 - 请假文件
tk.Label(root, text="选择请假文件:").grid(row=2, column=0, padx=10, pady=5, sticky="w")
entry_file3 = tk.Entry(root, width=45)
entry_file3.grid(row=2, column=1, padx=5, pady=5, sticky="ew")
tk.Button(root, text="浏览...", command=lambda: select_file(entry_file3)).grid(row=2, column=2, padx=5, pady=5)

# 文件选择4 - 节日文件
tk.Label(root, text="选择节日文件:").grid(row=3, column=0, padx=10, pady=5, sticky="w")
entry_file4 = tk.Entry(root, width=45)
entry_file4.grid(row=3, column=1, padx=5, pady=5, sticky="ew")
tk.Button(root, text="浏览...", command=lambda: select_file(entry_file4)).grid(row=3, column=2, padx=5, pady=5)

# 周六/周日选择
tk.Label(root, text="选择休息日:").grid(row=4, column=0, padx=10, pady=5, sticky="w")
var_day = tk.StringVar(value="周六")
frame_day = tk.Frame(root)
frame_day.grid(row=4, column=1, padx=5, pady=5, sticky="w")
tk.Radiobutton(frame_day, text="周六", variable=var_day, value="周六").pack(side="left")
tk.Radiobutton(frame_day, text="周日", variable=var_day, value="周日").pack(side="left")

# 年份输入
tk.Label(root, text="输入年份（仅数字）:").grid(row=5, column=0, padx=10, pady=5, sticky="w")
entry_year = tk.Entry(root, width=15)
entry_year.grid(row=5, column=1, padx=5, pady=5, sticky="w")
entry_year.insert(0, "")

# 月份输入
tk.Label(root, text="输入月份（仅数字）:").grid(row=6, column=0, padx=10, pady=5, sticky="w")
entry_month = tk.Entry(root, width=15)
entry_month.grid(row=6, column=1, padx=5, pady=5, sticky="w")
entry_month.insert(0, "")

# 生成按钮（居中显示）
btn_frame = tk.Frame(root)
btn_frame.grid(row=7, column=0, columnspan=3, pady=15)
tk.Button(btn_frame, text="生成数据", command=generate_output,
          width=20, height=2, bg="#4CAF50", fg="white").pack()

# 运行主循环
root.mainloop()
